/* store */
import { useBaseSelectUserStoreUserListMap, useBaseSelectUserStoreLoadingMap, useBaseSelectUserStoreProjectTeamList, useBaseSelectUserStoreFetchProjectTeamList, useBaseSelectUserStoreFetchProjectTeamUserList } from '@src/component/common/BaseSelectUser/store';

/* model */
import { BaseSelectUserErrorMessageEnum, SelectUserComponentProjectTeam } from '@src/component/common/BaseSelectUser/model';

/* hooks */
import { useBaseSelectUserOptions } from '@src/component/common/BaseSelectUser/hooks/options';

import MsgModel from '@model/MsgModel';

import { message } from '@src/util/message';

import i18n from '@src/locales'

/**
 * @description 获取选人组件 角色列表
 */
function useFetchProjectTeamList() {
  const { setProjectTeamList } = useBaseSelectUserStoreProjectTeamList();
  const { setProjectTeamLoading, clearProjectTeamLoading } = useBaseSelectUserStoreLoadingMap();

  const { fetchProjectTeamList: optionsFetchProjectTeamList } = useBaseSelectUserOptions();
  const fetchProjectTeamList = useBaseSelectUserStoreFetchProjectTeamList();

  const fetchList = optionsFetchProjectTeamList.value || fetchProjectTeamList;

  const fetchProjectTeamListImpl = () => {
    setProjectTeamLoading();

    return fetchList()
      .then(result => {
        if (MsgModel.isSuccess(result)) {
          let newSelect = (result?.data || []).map(item => {
            return {
              id: item.id,
              name: item.name === 'all' ? i18n.t('common.base.all') : item.name,
              userCount: item.userCount
            }
          })
          setProjectTeamList(newSelect);
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.projectTeamList);
        }
      })
      .finally(() => {
        clearProjectTeamLoading();
      });
  };

  return fetchProjectTeamListImpl;
}

/**
 * @description 获取选人组件 项目组 人员列表
 */
function useFetchProjectTeamUserList() {
  const { setProjectTeamUserList } = useBaseSelectUserStoreUserListMap();

  const { setProjectTeamUserLoading, clearProjectTeamUserLoading } = useBaseSelectUserStoreLoadingMap();

  const { fetchProjectTeamUserList: optionsFetchProjectTeamUserList } = useBaseSelectUserOptions();
  const fetchProjectTeamList = useBaseSelectUserStoreFetchProjectTeamList();

  const fetchProjectTeamUserList = optionsFetchProjectTeamUserList.value || fetchProjectTeamList;

  const fetchProjectTeamUserListImpl = (isInit: boolean = false) => {

    setProjectTeamUserLoading();

    return fetchProjectTeamUserList()
      .then(result => {
        if (MsgModel.isSuccess(result)) {
          let userList = (result?.data || []).map((item: SelectUserComponentProjectTeam) => {
            return {
              userId: item?.userId,
              staffId: item?.staffId,
              displayName: item?.displayName,
              head: item?.head,
              typeId: 1, // 暂时默认就是用户，不定义新的类型了
            }
          })

          setProjectTeamUserList(userList);
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.projectTeamList);
        }
      })
      .finally(() => {
        clearProjectTeamUserLoading();
      });
  };

  return fetchProjectTeamUserListImpl;
}

export { useFetchProjectTeamList, useFetchProjectTeamUserList };
